package com.tiger.project.service.impl;

import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.redis.RedisCache;
import com.tiger.common.utils.Code;
import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.project.domain.Asset;
import com.tiger.project.domain.BookValue;
import com.tiger.project.domain.BookValueEvaluate;
import com.tiger.project.mapper.AssetMapper;
import com.tiger.project.mapper.BookValueEvaluateMapper;
import com.tiger.project.mapper.BookValueMapper;
import com.tiger.project.service.IBookValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;



/**
 * 【账面价值】Service业务层处理
 *
 * @author standard
 * @date 2022-09-15
 */
@Service
public class BookValueServiceImpl implements IBookValueService {
    @Autowired
    private BookValueMapper bookValueMapper;
    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private BookValueEvaluateMapper evaluateMapper;

    /**
     * 查询【账面价值】
     *
     * @param id 【账面价值】主键
     * @return 【账面价值】
     */
    @Override
    public BookValue selectBookValueById(Long id) {
        BookValue bookValue = bookValueMapper.selectBookValueById(id);
        if (StringUtils.isNotEmpty(bookValue.getAssetPictures()))
            bookValue.setAssetPicturesList(Arrays.asList(bookValue.getAssetPictures().split(",")));

        // 资产
        String assetId = bookValue.getAssetId();
        if (StringUtils.isNotEmpty(assetId)){
            List<String> ids = Arrays.asList(assetId.split(","));
            for (String i : ids) {
                Asset asset = assetMapper.selectAssetById(Long.valueOf(i));
                bookValue.getAssetList().add(asset);
            }
        }

        //估价
        BookValueEvaluate bookValueEvaluate = new BookValueEvaluate();
        bookValueEvaluate.setBookValueId(id);
        List<BookValueEvaluate> bookValueEvaluateList = evaluateMapper.selectBookValueEvaluateList(bookValueEvaluate);
        if (bookValueEvaluateList != null && bookValueEvaluateList.size() > 0){
            bookValue.getEvaluateList().addAll(bookValueEvaluateList);
        }
        return bookValue;
    }

    /**
     * 查询【账面价值】列表
     *
     * @param bookValue 【账面价值】
     * @return 【账面价值】
     */
    @Override
    public List<BookValue> selectBookValueList(BookValue bookValue) {
        return bookValueMapper.selectBookValueList(bookValue);
    }

    /**
     * 新增【账面价值】
     *
     * @param bookValue 【账面价值】
     * @return 结果
     */
    @Override
    public int insertBookValue(BookValue bookValue) {
        int code = redisCache.getCacheObjectCode(LocalDate.now().format(Code.fmt) + "zm");
        addCode(code);
        bookValue.setBookValueNo(String.format("%s%s%04d", "ZMJZ","-", code));
        bookValue.setCreatedAt(DateUtils.getNowDate());
        bookValue.setIsDeleted(0L);
        changeArea(bookValue);
        int i = bookValueMapper.insertBookValue(bookValue);
        change(bookValue);
        return i;
    }

    /**
     * 修改【账面价值】
     *
     * @param bookValue 【账面价值】
     * @return 结果
     */
    @Override
    public int updateBookValue(BookValue bookValue) {
        Asset asset1 = new Asset();
        asset1.setBookValueId(bookValue.getId());
        List<Asset> assets = assetMapper.selectAssetList(asset1);
        if (assets != null && assets.size() > 0){
            for (Asset a : assets){
                a.setId(a.getId());
                asset1.setBookValueId(null);
                assetMapper.updateAsset(asset1);
            }
        }
        change(bookValue);
        bookValue.setUpdatedAt(DateUtils.getNowDate());
        changeArea(bookValue);
        return bookValueMapper.updateBookValue(bookValue);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBookValueByIds(Long[] ids) {
        for (Long id : ids){
            Asset asset = new Asset();
            asset.setBookValueId(id);
            List<Asset> assets = assetMapper.selectAssetList(asset);
            //删除账面价值，把资产关联的该账面价值置空
            if (assets != null && assets.size() > 0){
                for (Asset a : assets){
                    a.setId(a.getId());
                    a.setBookValueId(null);
                    assetMapper.updateAsset(a);
                }
            }
        }
        return bookValueMapper.deleteBookValueByIds(ids);
    }

    /**
     * 删除【账面价值】信息
     *
     * @param id 【账面价值】主键
     * @return 结果
     */
    @Override
    public int deleteBookValueById(Long id) {
        return bookValueMapper.deleteBookValueById(id);
    }


    /**
     *  水电总表关联账面列表
     */
    @Override
    public List<BookValue> getBookValue(LoginUser loginUser,Long id) {
        return bookValueMapper.getBookValue(loginUser.getDeptId(),id);
    }


    /**
     * 生成编码
     */
    private synchronized void addCode(int code) {
        redisCache.setCacheObject(LocalDate.now().format(Code.fmt) + "zm", code + 1);
    }

    //账面价值下关联资产的总面积
    private void changeArea(BookValue bookValue){
        BigDecimal realArea = new BigDecimal(0);
        // 资产面积
        String assetId = bookValue.getAssetId();
        if (StringUtils.isNotEmpty(assetId)){
            List<String> ids = Arrays.asList(assetId.split(","));
            for (String i : ids) {
                Asset asset = assetMapper.selectAssetById(Long.valueOf(i));
                BigDecimal area = asset.getPropertyArea();
                realArea = realArea.add(area);
                bookValue.getAssetList().add(asset);
            }
            bookValue.setTotalPropertyArea(realArea);
        }
    }

    private void change(BookValue bookValue){
        // 资产
        String assetId = bookValue.getAssetId();
        if (StringUtils.isNotEmpty(assetId)){
            List<String> ids = Arrays.asList(assetId.split(","));
            for (String i : ids) {
                Asset a = assetMapper.selectAssetById(Long.valueOf(i));
                a.setBookValueId(bookValue.getId());
                a.setId(a.getId());
                assetMapper.updateAsset(a);
            }
        }

        //估价
        evaluateMapper.deleteBookValueEvaluateByBookId(bookValue.getId());
        if (StringUtils.isNotEmpty(bookValue.getEvaluateList())){
            bookValue.getEvaluateList().forEach(item ->{
                item.setBookValueId(bookValue.getId());
                evaluateMapper.insertBookValueEvaluate(item);
            });
        }

    }

}
